package thread.condition;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * condition 能实现选择性通知
 *
 * @author liweimin
 * @date 2021/5/8 15:50
 * @since
 */
public class Test01 {

    public static void main(String[] args) throws InterruptedException {
        new SubThread().start();
        TimeUnit.SECONDS.sleep(3);
        try {
            lock.lock();
            condition.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    static Lock lock = new ReentrantLock();

    static Condition condition = lock.newCondition();

    static class SubThread extends Thread {
        @Override
        public void run() {
            try {
                lock.lock();
                System.out.println("method lock");
                condition.await();
                System.out.println("method await");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println("method unlock");
            }
        }
    }
}
